<?php
// $Id$

/**
 * Contains class Tree_Menu_ML_DBC_File_Importer
 *
 * @package modules
 * @subpackage tree_menu_ml
 */

/**
 *
 */
require_once($WSC->get_site_inc_dir().'modules/tree_menu/tree_menu_dbc_file_importer.class.php');
/**
 *
 */
require_once('tree_menu_ml_dbc.class.php');
/**
 *
 */
require_once('tree_menu_item_ml_dbc.class.php');


/**
 * Tree_Menu_ML_DBC_File_Importer is used to import multilanguage tree menu items from XML file
 *
 * Example for importing new items:
 *
 * <?xml version="1.0" encoding="utf-8"?>
 * <menuimportfile>
 * 	<menuimport>
 * 		<menuitem>
 * 			<hid>test_item</hid>
 * 			<parenthid>Core</parenthid>
 * 			<label>Test</label>
 * 			<link>test.php</link>
 * 			<target>_blank</target>
 * 			<order>100</order>
 * 			<mllabels>
 * 				<mllabel>
 * 					<lang>bg</lang>
 * 					<value>Тест</value>
 * 				</mllabel>
 * 			</mllabels>
 * 		</menuitem>
 * 	</menuimport>
 * </menuimportfile>

 *
 * Example for deleting menu items:
 *
 * <?xml version="1.0" encoding="utf-8"?>
 * <menuimportfile>
 * 	<menuitemsdelete>
 * 		<itemhid>presni</itemhid>
 * 	</menuitemsdelete>
 * </menuimportfile>
 */
class Tree_Menu_ML_DBC_File_Importer extends Tree_Menu_DBC_File_Importer {
	private $default_lang;
	private $lang_arr = array();

	public function import(DB_Connection $dbc,
							Tree_Menu_DBC $menu,
							$import_dir,
							$update_existing = false,
							$db_tables_prefix,
							$default_lang = 'en',
							$lang_arr = array()) {

		$ret = false;

		if ($menu instanceof Tree_Menu_ML_DBC) {
			$this->default_lang = $default_lang;
			$this->lang_arr = $lang_arr;

			$ret = parent::import($dbc, $menu, $import_dir, $update_existing, $db_tables_prefix);
		} else {
			throw new TE_Tree_Menu('$menu is not instance of Tree_Menu_ML_DBC');
		}

		return $ret;
	}


	public function get_default_lang() {
		return $this->default_lang;
	}


	public function import_from_file($file) {
		$ret = false;

		$xml = @simplexml_load_file($file);

		if ($xml) {
			if ($xml->menuimport) {
				$imported_items = array();
				foreach($xml->menuimport->menuitem as $item) {
					if (!trim($item->label)) {
						throw new TE_Tree_Menu('Empty or missing label.');
					}
					$raw_arr[0] = trim($item->label);
					$raw_arr[1] = trim(strtolower($item->parenthid));
					$raw_arr[2] = trim($item->link);
					if (!trim($item->hid)) {
						throw new TE_Tree_Menu('Empty or missing hid.');
					}
					$raw_arr[3] = trim($item->hid);
					$raw_arr[4] = trim($item->target);
					$raw_arr[5] = trim($item->order);

					$raw_arr['ml_labels'] = array();
					if ($item->mllabels) {
						$labels = array();
						$c = 0;
						foreach($item->mllabels->children() as $label) {
							$labels[$c]['lang'] = $this->fetch_lang(trim($label->lang));
							$labels[$c]['value'] = trim($label->value);

							$c++;
						}
						$raw_arr['ml_labels'] = $labels;
					}

					$imported_items[] = Tree_Menu_Item_ML_DBC::create_from_import_data($raw_arr, $this->get_db_tables_prefix().'menu', $this->get_update_existing(), $this->get_default_lang());
					$ret['new_items'] = $imported_items;
				}
			} elseif ($xml->menuitemsdelete) {
				foreach($xml->menuitemsdelete->itemhid as $itemhid) {
					$ret['delete_items'][] = trim($itemhid);
				}
			}
		} else {
			throw new TE_Tree_Menu_DBC_Import_File_Failed('File '.$file.' is not valid import XML file.');
		}


		return $ret;
	}


	private function fetch_lang($lang_code) {
		if (array_key_exists($lang_code, $this->lang_arr)) {
			$ret = $this->lang_arr[$lang_code]['id'];
		} else {
			throw new TE_Tree_Menu('Cannot find language '.$lang_code.' in $this->lang_arr.');
		}

		return $ret;
	}


	protected function delete_item(DB_Connection $dbc, Tree_Menu_DBC $menu, $item_hid, $db_tables_prefix = '') {

		$item = $menu->find_item($item_hid);
		if ($item) {
			$id = $item->get_id();
			if ($id) {
				Tree_Menu_Item_ML_DBC::delete_cascade($dbc, $id, $db_tables_prefix);
			}
		}
	}
}


